﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using Utilities.QuickSql;
using MySql.Data;

namespace Deviseur_Cs2i_Car
{
    public partial class AddVoiOption : Form
    {
        VoiOption voiOption;

        public AddVoiOption()
        {
            InitializeComponent();

            this.Text = "Ajouter une voiture to option";
            this.buttonAddVoiOption.Text = "Ajouter";
            /*******/
            DataTable DataVoi = new DataTable();

            DataVoi.Columns.Add("IDVoi");
            DataVoi.Columns.Add("NomVoi");
            DataVoi.Columns.Add("NBPortes");
            DataVoi.Columns.Add("PrixBaseVoi");
            DataVoi.Columns.Add("NumCouleurCaross");
            DataVoi.Columns.Add("NumType");
            DataVoi.Columns.Add("ImageVoi");
            DataVoi.Rows.Add("0", "- Choisir une Voiture -");

            Table couleur = Program.Db.QueryTable("SELECT * FROM VOITURE");
            foreach (Record r in couleur)
                DataVoi.Rows.Add(r.Values[0], r.Values[1]);

            this.comboBoxVoiture.DataSource = DataVoi;
            this.comboBoxVoiture.ValueMember = "IDVoi";
            this.comboBoxVoiture.DisplayMember = "NomVoi";


            this.comboBoxVoiture.SelectedIndex = 0;
            /*******/
            DataTable DataOption = new DataTable();

            DataOption.Columns.Add("IDOption");
            DataOption.Columns.Add("LibelleOption");
            DataOption.Columns.Add("PrixOption");
            DataOption.Columns.Add("NumCateg");
            DataOption.Rows.Add("0", "- Choisir une option -");

            Table type = Program.Db.QueryTable("SELECT * FROM OPTIONS");
            foreach (Record r in type)
                DataOption.Rows.Add(r.Values[0], r.Values[1]);

            this.comboBoxOption.DataSource = DataOption;
            this.comboBoxOption.ValueMember = "IDOption";
            this.comboBoxOption.DisplayMember = "LibelleOption";

            this.comboBoxOption.SelectedIndex = 0;
        }
        public AddVoiOption(int NumVoi, double NumOption)
        {
            InitializeComponent();
            
            this.Text = "Modifier une voiture to option";
            this.buttonAddVoiOption.Text = "Modifier";

            DataTable DataVoi = new DataTable();

            DataVoi.Columns.Add("IDVoi");
            DataVoi.Columns.Add("NomVoi");
            DataVoi.Columns.Add("NBPortes");
            DataVoi.Columns.Add("PrixBaseVoi");
            DataVoi.Columns.Add("NumCouleurCaross");
            DataVoi.Columns.Add("NumType");
            DataVoi.Columns.Add("ImageVoi");
            DataVoi.Rows.Add("0", "- Choisir une Voiture -");

            Table couleur = Program.Db.QueryTable("SELECT * FROM VOITURE");
            foreach (Record r in couleur)
                DataVoi.Rows.Add(r.Values[0], r.Values[1]);

            this.comboBoxVoiture.DataSource = DataVoi;
            this.comboBoxVoiture.ValueMember = "IDVoi";
            this.comboBoxVoiture.DisplayMember = "NomVoi";

            this.comboBoxVoiture.SelectedIndex = 0;

            DataTable DataOption = new DataTable();

            DataOption.Columns.Add("IDOption");
            DataOption.Columns.Add("LibelleOption");
            DataOption.Columns.Add("PrixOption");
            DataOption.Columns.Add("NumCateg");
            DataOption.Rows.Add("0", "- Choisir une option -");

            Table type = Program.Db.QueryTable("SELECT * FROM OPTIONS");
            foreach (Record r in type)
                DataOption.Rows.Add(r.Values[0], r.Values[1]);

            this.comboBoxOption.DataSource = DataOption;
            this.comboBoxOption.ValueMember = "IDOption";
            this.comboBoxOption.DisplayMember = "LibelleOption";

            Table toto = Program.Db.QueryTable("SELECT * FROM Voi_Option WHERE NumVoi="+NumVoi+" AND NumOption ="+NumOption);
            foreach (Record r in toto)
            {
                this.comboBoxVoiture.SelectedValue = r.Values[0].ToString();
                this.comboBoxOption.SelectedValue = r.Values[1].ToString();
                voiOption = new VoiOption(Convert.ToInt32(this.comboBoxVoiture.SelectedValue), Convert.ToDouble(this.comboBoxOption.SelectedValue));
            }

            this.comboBoxOption.SelectedIndex = 0;
           
            
        }
        private void buttonAddVoiOption_Click(object sender, EventArgs e)
        {
            if (comboBoxVoiture.SelectedValue.ToString() != "0" && comboBoxOption.SelectedValue.ToString() != "0")
            {
                if (this.buttonAddVoiOption.Text == "Ajouter")
                {
                    voiOption = new VoiOption(Convert.ToInt32(this.comboBoxVoiture.SelectedValue.ToString()), Convert.ToInt32(this.comboBoxOption.SelectedValue.ToString()));

                    Thread addThread = new Thread(new ThreadStart(addVoiture_Thread));
                    addThread.Start();
                }
                else if (this.buttonAddVoiOption.Text == "Modifier")
                {
                    voiOption.set_NumVoi(Convert.ToInt32(this.comboBoxVoiture.SelectedValue.ToString()));
                    voiOption.set_NumOption(Convert.ToInt32(this.comboBoxOption.SelectedValue.ToString()));
                    Thread updateThread = new Thread(new ThreadStart(updateType_Thread));
                    updateThread.Start();
                }
            }
            else
            {
                MessageBox.Show("Veuillez remplir tous les champs", "Erreur de saisie");
            }
        
        }
        private void addVoiture_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            voiOption.new_VoiOption();
            Invoke(new MethodInvoker(CloseFen));
        }
        private void updateType_Thread()
        {
            Invoke(new MethodInvoker(Disabled));
            voiOption.modify_VoiOption();
            Invoke(new MethodInvoker(CloseFen));
        }

        private void Disabled()
        {
            this.comboBoxVoiture.Enabled = false;
            this.comboBoxOption.Enabled = false;
            this.buttonAddVoiOption.Enabled = false;
        }
        private void CloseFen()
        {
            this.Close();
        }
    }
}
